package com.wanxi.dao.user.Impl;

import com.wanxi.util.JDBC;

public class UserDaoImpl  {
    //数据库连接类
    JDBC jdbc = new JDBC();

    //根据用户名密码查询
    /*public UserModel selectByNameAndPassword(String name, String password) {
        String sql = "select * from user where username = ? and password = ?";

        List<Object> data = new ArrayList<>();
        data.add(name);
        data.add(password);

        ResultSet userInfo = jdbc.findByNameAndPass(sql,data);

        //用户实体类
        UserModel user = new UserModel();

        while (true) {
            try {
                if (!userInfo.next()) break;
                user.setId(userInfo.getInt("id"));
                user.setUsername(userInfo.getString("username"));
                user.setSex(userInfo.getString("sex"));
                user.setPhone(userInfo.getString("phone"));
                user.setAddress(userInfo.getString("address"));
                user.setEnable(userInfo.getInt("enable"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }*/

    //查询所有用户
    /*public List<UserModel> selectAll(PageModel pageModel) {
        String sql = "select * from user limit ?,?";

        List<Object> data = new ArrayList<>();
        data.add((pageModel.getCurr()-1)*pageModel.getLimit());
        data.add(pageModel.getLimit());

        ResultSet users = jdbc.findAll(sql, data);

        List<UserModel> userModels = new ArrayList<>();
        //遍历数据集，取出所有用户信息
        try {
            while (users.next()) {
                UserModel userModel = new UserModel();
                userModel.setId(users.getInt("id"));
                userModel.setUsername(users.getString("username"));
                userModel.setSex(users.getString("sex"));
                userModel.setPhone(users.getString("phone"));
                userModel.setAddress(users.getString("address"));
                userModel.setEnable(users.getInt("enable"));

                userModels.add(userModel);
            }
            return userModels;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;

    }*/

    //插入数据
   /* public int insertUser(UserModel userModel) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = simpleDateFormat.format(new Date());

        List<Object> data = new ArrayList<>();

        String sql = "insert into user (" +
                "username,password,phone" +
                "sex,hobby,birthday," +
                "enable,create_time,update_time,email) values " +
                "(?,?,?,?,?,?,?,?,?,?)";

        data.add(userModel.getUsername());
        data.add(userModel.getPassword());
        data.add(userModel.getPhone());
        data.add(userModel.getSex());
        data.add(userModel.getHobby());
        data.add(userModel.getBirthday());
        data.add(userModel.getEnable());
        data.add(now);
        data.add(now);
        data.add(userModel.getEmail());

        int num = jdbc.insertUser(sql,data);
        return num;
    }*/

    //跟据用户id查询数据
   /* public UserModel selectById(UserModel userModel) {

        String sql = "select * from user where id = ?";

        List<Object> data = new ArrayList<>();
        data.add(userModel.getId());

        ResultSet resultSet = jdbc.findByID(sql,data);

        UserModel user = new UserModel();

        while (true) {
            try {
                if (!resultSet.next()) break;
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPhone(resultSet.getString("phone"));
                user.setEmail(resultSet.getString("email"));
                user.setHobby(resultSet.getString("hobby"));
                user.setEnable(resultSet.getInt("enable"));
                user.setPassword(resultSet.getString("password"));
                user.setSex(resultSet.getString("sex"));
                user.setBirthday(resultSet.getString("birthday"));

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }*/

    //更新数据
    /*public int update(UserModel user) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String now = simpleDateFormat.format(new Date());

        List<Object> data = new ArrayList<>();

        String sql = "update user  " +
                "set username = case when 'null' = '" +user.getUsername()+"' then username else ? end,"+
                " email = case when 'null' = '" +user.getEmail()+"' then email else ? end," +
                " phone = case when 'null' = '" +user.getPhone()+"' then phone else ? end,"+
                "hobby = case when 'null' = '" +user.getHobby()+"' then hobby else ? end,"+
                "sex = case when 'null' = '" +user.getSex()+"' then sex else ? end,"+
                "birthday = case when 'null' = '" +user.getBirthday()+"' then birthday else ? end,"+
                "enable = case when 'null' = '" +user.getEnable()+"' then enable else ? end,"+
                "password = case when 'null' = '" +user.getPassword()+"' then password else ? end,"+
                "update_time = ? " +
                "where id = ? ";

        data.add(user.getUsername());
        data.add(user.getEmail());
        data.add(user.getPhone());
        data.add(user.getHobby());
        data.add(user.getSex());
        data.add(user.getBirthday());
        data.add(user.getEnable());
        data.add(user.getPassword());
        data.add(now);
        data.add(user.getId());

        int update = jdbc.update(sql,data);
        return update;
    }*/

    //删除数据
   /* public int deleteById(UserModel userModel) {

        List<Object> data = new ArrayList<>();

        String sql = "delete from user where id = ?";

        data.add(userModel.getId());

        int num = jdbc.deleteById(sql,data);
        return num;
    }*/

    //获取记录条数
    /*public int getCount() {

        String sql = "select count(*) count from user";

        int count = jdbc.getCount(sql);
        return count;
    }*/

    //模糊查询
    
    /*public List<UserModelExtends> globalSearch(UserModelExtends userModelExtends, PageModel pageModel) {

        String sql = "select * from user where 1=1";
        if(userModelExtends.getUsername()!=null && !(userModelExtends.getUsername().equals(""))){
            sql+= " and username like '%"+userModelExtends.getUsername()+"%'";
        }
        if(userModelExtends.getStartDate()!=null && !(userModelExtends.getStartDate().equals("")) ){
            sql+= " and create_time >= '"+userModelExtends.getStartDate()+"'";
        } if(userModelExtends.getEndDate()!=null && !(userModelExtends.getEndDate().equals(""))){
            sql+= " and create_time <= '"+userModelExtends.getEndDate()+"'";
        }
        sql += " limit "+(pageModel.getCurr()-1)*pageModel.getLimit()+","+pageModel.getLimit();

        ResultSet resultSet = jdbc.GlobalSearch(sql);

        List<UserModelExtends> userModels =new ArrayList<>();

        try {
            while (resultSet.next()) {
                UserModelExtends userModelExtends1 = new UserModelExtends();

                userModelExtends1.setId(resultSet.getInt("id"));
                userModelExtends1.setUsername(resultSet.getString("username"));
                userModelExtends1.setSex(resultSet.getString("sex"));
                userModelExtends1.setPhone(resultSet.getString("phone"));
                userModelExtends1.setAddress(resultSet.getString("address"));
                userModelExtends1.setEnable(resultSet.getInt("enable"));

                userModels.add(userModelExtends1);

            }
            return userModels;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }*/

    //模糊查询得到的记录条数
    
  /*  public int getGlobalSeachCount(UserModelExtends userModelExtends) {
        String sql =  "select count(*) count from user where 1=1 ";

        if(userModelExtends.getUsername()!=null && !(userModelExtends.getUsername().equals(""))){
            sql+= " and username like '%"+userModelExtends.getUsername()+"%'";
        }
        if(userModelExtends.getStartDate()!=null && !(userModelExtends.getStartDate().equals("")) ){
            sql+= " and create_time >= '"+userModelExtends.getStartDate()+"'";
        } if(userModelExtends.getEndDate()!=null && !(userModelExtends.getEndDate().equals(""))){
            sql+= " and create_time <= '"+userModelExtends.getEndDate()+"'";
        }

        int globalSearchCount = jdbc.getGlobalSearchCount(sql);
        return globalSearchCount;
    }*/
}
